const util = require('../../utils/util.js')
const BL = require('../../utils/lanya.js')
var pos = require('../../utils/pos.js')
var GXZT;//更新状态
var that;

Page({
  data: {
    LYData: {
      openOK: false,//蓝牙是否打开
      BLZT: false,//蓝牙状态是否可用
      BLSO: false,//蓝牙是否正在搜索
      TS: ""      
    },
    LYSB: [],
    receiveData:"",
    is_Print_text_Disabled:true,
    is_Print_barcode_Disabled:true,
    is_Print_order_Disabled:true,
    is_Print_curve_Disabled:true,
    is_Print_Self_Disabled:true,
    is_search_Disabled:false,
  },
  
  onShow:function()
  {
    //BL.zdlj();
    that = this;
  },
  onUnload: function () 
  {
    //BL.stopBL();
  },
  openP: function (e) {
    BL.OpenPrint();
    clearTimeout(GXZT);
    tim(this);
  },
  
  lianjie: function (e) 
  {
    console.log("连接按钮", e)
    var uuid = e.currentTarget.dataset.uuid;
    BL.lianjie(uuid,function(res)
    {
          console.log(res);
          if ( res == "连接失败,请重新配置打印机" )
          {
              //显示信息框
              wx.showToast({title: "连接失败",icon: 'error',duration: 5000})
              //连接失败，打印文本，条码，指令，自检，可用
              that.setData({
                            is_Print_text_Disabled:true,
                            is_Print_barcode_Disabled:true,
                            is_Print_order_Disabled:true,
                            is_Print_Self_Disabled:true,
                            is_Print_curve_Disabled:true,
                            is_search_Disabled:false,
                        }) 
          }
          else
          {
              console.log('---------连接成功,启动接收监听------------');
              pos.startnotify(that);      // 监听接收事件
              //连接成功，打印文本，条码，指令，自检，可用
              that.setData({
                  is_Print_text_Disabled:false,
                  is_Print_barcode_Disabled:false,
                  is_Print_order_Disabled:false,
                  is_Print_curve_Disabled:false,
                  is_Print_Self_Disabled:false,
                  is_search_Disabled:true,
              })
          } 
    });
    //BL.dellb(e.currentTarget.dataset.ite);
  }, duankai: function (e) 
  {
    var uuid = e.currentTarget.dataset.uuid;
    BL.duankai(uuid,"no",function(res)
    {
        if ( res == "ok" )
        {
            console.log('----ok---ok----');
            // 断开，打印文本，条码，指令，自检，可用
            that.setData({
              is_Print_text_Disabled:true,
              is_Print_barcode_Disabled:true,
              is_Print_order_Disabled:true,
              is_Print_curve_Disabled:true,
              is_Print_Self_Disabled:true,
              is_search_Disabled:false,
          }) 
        }
    });
  },
  

  Printg: function () 
  {   
      printText();
  },

  Print_img:function()
  {
      printImg();
  },

  Print_barcode:function()
  {  
      printBarcode();
  },

  Print_order:function()
  {      
      printOrder();
  },

  Print_curve:function()
  {
       printCurve();
  },


  Print_Self:function()
  {   
      printSelf();
  }
})

function tim(that) 
{
  that.setData({
    LYData: BL.GETData(),
    LYSB: BL.GETSB()
  })  
 GXZT=setTimeout(function () { tim(that) }, 500)
}


function printText() 
{
  pos.AddPrintQueue('InitPos');           // 初始化蓝牙打印机 [27,64]
  pos.PrintTitle(" 超市名(可自定义)");//打印标题  
  pos.PrintNameValue('单号: ', 'ls0220180125000001'); 
  pos.PrintText("收银员: 1001    机器编号: 001");//打印标题  
  pos.PrintNameValue('时间: ', '2017-12-23 12:01:01');
  pos.PrintText("品名  数量   单价     小计"); 

  pos.PrintMiddleText('--------------------------------'); 
   pos.PrintText("可口可乐(听)");
   pos.PPP("2.0", "2", "4.00");
  pos.PrintText("大泽山葡萄(kg)");
   pos.PPP("12.8", "2.0", "25.6");
   pos.PrintText("大泽山葡萄(盒)");
   pos.PPP("30", "2.0", "60.0");
   pos.PrintMiddleText('--------------------------------');
   pos.PrintNameValue('合计数量: ', '6.0');
   pos.PrintNameValue('标价金额: ', '128.8');
   pos.PrintText('合计优惠(促销/特价等):  -2.3');
   pos.PrintNameValue('合计应收: ', '126.5');
   pos.PrintNameValue('实际收款: ', '126.5');
   pos.PrintNameValue('优惠(抹零): ', '0.5');
   pos.PrintMiddleText('--------------------------------');
   pos.PrintText("收款明细: ");//打印标题
   pos.PrintNameValue('现金: ', '100');
   pos.PrintNameValue('微信刷卡: ', '26.5');
   pos.PrintNameValue('零钱包支付: ', '0.5');
   pos.PrintNameValue('现金找零: ', '4.0');
   pos.PrintMiddleText('--------------------------------');
   pos.PrintNameValue('会员卡: ', '132****8588'); 
   pos.PrintNameValue('支付前余额: ', '5.00');
   pos.PrintNameValue('当前可用余额: ', '4.5');
   pos.PrintNameValue('本次积分: ', '12.0');
   pos.PrintNameValue('当前可用积分: ', '285.0');
   pos.PrintMiddleText('--------------------------------');
   pos.PrintText("谢谢您的惠顾,欢迎下次光临!");//打印标题
   pos.printTwoBarCode(pos.TWO_QRCODE,10, 8, 0x48, 'http://xxx.xxx.xxx');   
   pos.PrintJumpLines(3);

  pos.DaYin(function (res) 
  {
      if (res == "err") 
      {        
          pos.ClearQueue();//清空打印列队
          //提示,打印出错,请稍后再试,仍然出错请重新设置打印机
      }
  });
}

function printBarcode()
{
    pos.AddPrintQueue('InitPos');           // 初始化蓝牙打印机 [27,64]
    
    pos.PrintText("ONE_UPC_A");       
    pos.printOnBarCode(pos.ONE_UPC_A,2,63,1,'100000000007');   //11 ≤ k ≤ 12       
      
    pos.PrintText("ONE_UPC_E");       
    pos.printOnBarCode(pos.ONE_UPC_E,2,63,1,'100000000008');  //11 ≤ k ≤ 12    
    
    pos.PrintText("ONE_CODE39");    
    pos.printOnBarCode(pos.ONE_CODE39,2,63,1,'10009');  //1 ≤ k ≤ 255    
    
    pos.PrintText("ONE_ITF");    
    pos.printOnBarCode(pos.ONE_ITF,2,63,1,'11000099');  //ONE_ITF
        
    pos.PrintText("ONE_CODE93");    
    pos.printOnBarCode(pos.ONE_CODE93,2,63,1,'22000099');  //ONE_CODE93    
    
    pos.PrintText("ONE_JAN13");    
    pos.printOnBarCode(pos.ONE_JAN13,2,63,1,'012345678901');  //12 ≤ k ≤ 13   
    
    pos.PrintText("ONE_CODE128");    
    pos.printOnBarCode(pos.ONE_CODE128,2,63,1,'012345678901');  // 2 ≤ k ≤ 255
    
    pos.PrintText("ONE_JAN8");    
    pos.printOnBarCode(pos.ONE_JAN8,2,63,1,'01234567');  // 7≤ k ≤8   
    
    pos.PrintText("ONE_CODABAR");    
    pos.printOnBarCode(pos.ONE_CODABAR,2,63,1,'5012345675');  // 1 ≤ k ≤ 255    
    
    pos.PrintText("TWO_PDF417");    
    pos.printTwoBarCode(pos.TWO_PDF417,30, 8, 0, '000AA0000BB00000');   
    
    pos.PrintText("TWO_QRCODE");    
    pos.printTwoBarCode(pos.TWO_QRCODE,10, 8, 0x48, '000AA0000BB00000');
}

function printOrder()
{
    that.setData({ receiveData:""});
    pos.AddPrintQueue('InitPos');           // 初始化蓝牙打印机 [27,64]    
    pos.QueueWrite.push(pos.GS_Query_Printer_Speed);
    pos.DaYin(function (res) {});
}



//打印曲线
function printCurve()
{
    //pos.AddPrintQueue('InitPos');           // 初始化蓝牙打印机 [27,64]  
    //pos.PrintText("ONE_UPC_E"); 
    pos.printCurve([0x00],[0x168]);    
    pos.printCurveText(0,0,"000");
    for (  var i=2; i<=0xEF; i++ )
    {
         pos.printCurve( [0,i],[2,i+2]  );
    }
    pos.printCurveText(0,0,"150");
    for(var i=0xEF-1; i>=2; i--)
    {
        pos.printCurve([0,i],[2,i+2]);
    }
    pos.printCurveText(0,0,"300");
    for(var i=0;i<50; i++)
        pos.printCurve([0],[2]);
    pos.printCurveEnd();



    pos.printCurve([0x1E], [0x168]);
    pos.printCurve([0x1E], [0x168]);
    var y1=0;
    var y2=0;
    for (var i=1;i<1200;i++)
    {
        y1 = Math.sin(i/180.0*3.1416)*(380.0-30.0)/2.0+180.0; //计算sin函数坐标点
        y2 = Math.cos(i/180.0*3.1416)*(380.0-30.0)/2.0+180.0; //计算cos函数坐标点
        pos.printCurve([180,y1,y2], [182,y1+2,y2+2] );
    }
    pos.printCurveEnd();

}
/*
    function PrintText(text) 
{
    //AddPrintQueue('InitPos');
    AddPrintQueue('Print', text);
    AddPrintQueue('PrintJumpLine', 1);
}
*/




function printSelf()
{
    pos.AddPrintQueue('InitPos');           // 初始化蓝牙打印机 [27,64]
    pos.QueueWrite.push(pos.GS_Self_Check);
    pos.DaYin(function (res) {});
    pos.PrintJumpLines(2);
}

